package com.china08.yxyapi.util;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

/***
 * 云校云存储平台工具类**
 * 
 * @author Ertao.Fang
 *
 */
@Component
public class YXCSPUtil {

    // private static final Logger LOGGER =
    // LoggerFactory.getLogger(YXCSPUtil.class);
    //
    // @Value("${spring.profiles.active}")
    // private String profile;
    //
    // @Value("${app.global.yxcspBasePathPush}")
    // private String pushPath;

    // private ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private YxyOscUtil YxyOscUtil;

    /**
     * 上传
     *
     * @param file
     * @throws IOException
     */
    public String push(MultipartFile file) throws IOException {
        // long pushTimeB = System.currentTimeMillis();
        // LOGGER.info(StringUtils.repeat("*", 25));
        // LOGGER.info("上传开始...");
        // // HttpClient
        // CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // HttpPost httpPost = new HttpPost(pushPath);
        // String md5 = DigestUtils.md5Hex(file.getBytes());
        // // Set Headers
        // httpPost.addHeader("x-yx-csp-u", "WEIKE_IMG_UPLOAD_USER");
        // httpPost.addHeader("x-yx-csp-p", "783a8747635da85ae60118ecfdc45cdf");
        // httpPost.addHeader("x-yx-csp-md5", md5);
        // httpPost.addHeader("x-yx-csp-env", profile + "-weike");
        // // Creat Builder
        // MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        // builder.setCharset(Consts.UTF_8); // 防止filename中文乱码
        // builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//
        // 防止filename中文乱码
        // builder.addBinaryBody("attachment", file.getBytes(),
        // ContentType.APPLICATION_OCTET_STREAM,
        // file.getOriginalFilename());
        // HttpEntity httpEntity = builder.build();
        // httpPost.setEntity(httpEntity);
        // // Send Post
        // CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        // // Read Response
        // String content =
        // IOUtils.toString(httpResponse.getEntity().getContent(), "UTF-8");
        // LOGGER.info("上传结果:" + content);
        // // Check Response
        // Map<?, ?> contentMap = objectMapper.readValue(content, Map.class);
        // Integer status = (Integer) contentMap.get("status");
        // String msg = (String) contentMap.get("msg");
        // String fileUrl = (String) contentMap.get("fileUrl");
        // if (status == 0) {
        // Assert.isTrue(false, msg);
        // }
        // long pushTimeE = System.currentTimeMillis();
        // LOGGER.info("上传结束:用时:[" + (pushTimeE - pushTimeB) + "ms]");
        // LOGGER.info(StringUtils.repeat("*", 25));
        // return fileUrl;
        String fileUrl = YxyOscUtil.push(file);
        return fileUrl;
    }
}
